<template>
    <div>
        <div style="padding: 1px 0">
          <el-input
            style="width: 200px; font-size:medium"
            prefix-icon="el-icon-present"
            placeholder="请输入公告标题"
            v-model="name"
          ></el-input>
          <el-button class="ml-5" type="primary" @click="load" style="font-size:medium" >搜索</el-button>
          <el-button class="ml-5" type="info" @click="reset" style="font-size:medium">重置</el-button>
        </div>

        <div style="padding: 5px 0; margin:-10px 0 10px 937px">
          <el-button class="el-icon-circle-plus-outline" type="primary" @click="handleAdd" style="font-size:medium"><span style="font-size:20px;">添加</span></el-button>
          <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要批量删除吗?"
                @confirm="handleDeleteBatch">
          <el-button type="danger" slot="reference" style="font-size:medium"><span style="font-size:20px;">批量删除</span></el-button>
        </el-popconfirm>
          <!-- <el-button class="ml-5" type="primary" @click="exp" style="font-size:medium"><span style="font-size:20px;">导出</span></el-button> -->
        </div>

        <el-table :data="tableData" :height="Height" border stripe :header-cell-class-name="'headerBg4'" @selection-change="handleSelectionChange" style="font-size:large; margin-top:-10px;">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            prop="title"
            label="公告标题"
            width="330"
            align="center"
          ></el-table-column>
          <!-- <el-table-column
            type="number"
            prop="price"
            label="套餐价格"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="code"
            label="套餐编码"
            width="160"
            align="center"
          ></el-table-column> -->
          <el-table-column
            prop="createTime"
            label="创建时间"
            width="234"
            align="center"
          ></el-table-column>
          <el-table-column prop="status" label="公告内容" width="125" align="center">
            <template v-slot="scope">
              <el-button style="background-color: pink; color:brown" @click="showDetail(scope.row.id)"><span style="font-size:17px">查看</span></el-button>
            </template>
          </el-table-column>

          <el-table-column
            prop="createUser"
            label="创建人"
            width="88"
            align="center"
          ></el-table-column>
          <el-table-column prop="status" label="公告类型" width="110" align="center">
            <template v-slot="scope">
              <el-tag type="warning" size="large"><span style="font-size:16px; font-weight:600">{{ scope.row.type }}</span></el-tag>
           </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template slot-scope="scope">
              <el-button type="success"
              @click="handleEdit(scope.row)"><span style="font-size:15px">编辑 <i class="el-icon-edit"></i></span></el-button>

              <el-popconfirm
                class="ml-5"
                confirm-button-text='确定删除'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定要删除吗?"
                @confirm="handleDel(scope.row.id)"
                >
              <el-button type="danger" slot="reference"><span style="font-size:15px">删除 <i class="el-icon-remove-outline"></i></span></el-button>
            </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <div style="padding: 10px 0; margin-left:300px; margin-top:15px;">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[7, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>

        <el-dialog title="公告信息" :visible.sync="dialogFormVisible" width="55%">
          <el-form label-width="90px" size="medium">
            <el-form-item label="公告标题：" style="margin-left: 70px">
              <el-input v-model="form.title" autocomplete="off" placeholder="请输入公告标题" style="width: 420px"></el-input>
            </el-form-item>
            <el-form-item label="公告内容：" style="margin-left: 70px">
              <el-input style="width: 420px"  type="textarea" :rows="5" placeholder="请输入公告内容" v-model="form.content"> </el-input>
            </el-form-item>
            <el-form-item label="公告类型：" style="margin-left: 70px">
              <el-select clearable v-model="form.type" placeholder="请选择公告类型" style="width: 30%">
                  <el-option v-for="item in types" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="销售状态：">
              <el-input v-model="form.status" autocomplete="off"></el-input>
            </el-form-item> -->
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="addNotice">确 定</el-button>
          </div>
        </el-dialog>

        <el-dialog title="公告详情" :visible.sync="showContent" width="55%">
          <el-row style="margin:5px 0 0 100px">
                            <el-col :span="20">
                                <el-card style="height:320px; width:100%">
                                    <div style="width:550px; margin-top:30px">
                                        <span style="color:palevioletred; font-size:24px; margin-left: 60px; margin-top: 80px">{{ noticeDetail.title }}</span>
                                        <P style="font-size:18px;color:brown;margin: 30px 0 0 20px; width:450px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ noticeDetail.content }}
                                        </P>
                                        <!-- <el-button @click="back" type=“text” style="float:right; color:chocolate; border: none; margin-top:30px"><span style="font-size: 25px">返回>></span></el-button> -->
                                    </div>

                                </el-card>
                            </el-col>
                        </el-row>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="back"><span style="font-size:20px">确 定</span></el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
  name: 'Notice',
  data(){
    return{
        tableData: [],
        total: 0,
        pageNum: 1,
        pageSize: 7,
        name: "",
        dialogFormVisible: false,
        multipleSelection: [],
        form:{},
        isEdit: false,
        types: ['普通公告','活动公告','促销公告'],
        showContent: false,
        noticeDetail:{},
        isEdit: false,
        Height: '',
    }
  },
  created(){
    this.getHeight();
    this.load();
  },
  methods:{
    load() {
        this.request.get("/notice/pageAll",{
          params :{
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
            // code: this.code
          }
        }).then(res =>{
          console.log('res',res);
          console.log('res.records',res.data);
          console.log('res.total',res.total);
          this.tableData = res.data.records
          this.total = res.data.total
        })
      },
      getHeight(){
          //获取屏幕高度
          let screenHeight = document.documentElement.clientHeight - 281 + "px";
          this.$nextTick(() => {
          this.Height = screenHeight;
          })
        },
      back(){
        this.showContent = false;
      },
      showDetail(id){
        this.request.get('/notice/getById/'+id).then(res =>{
          if(res.code === '200'){
            console.log('对对对',res);
            this.noticeDetail = res.data;
          }
        })
        console.log('啧啧啧',this.form);
        console.log('喵喵喵',id);
        this.showContent = true;
      },
      addNotice(){
        console.log('嘿嘿嘿',this.form);
        this.request.post('/notice/add',this.form).then(res =>{
          console.log('嘻嘻嘻',res);
          if(res.code === '200'){
            if(this.isEdit === false){
              this.$message.success('添加公告成功!')
              this.load()
            }else{
              this.$message.success('修改公告成功!')
              this.load()
            }
            this.dialogFormVisible = false;
          }else{
            this.$message.success('添加公告失败!')
          }
        })
      },
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum;
      this.load();
    },
    reset() {
       this.name = "",
       this.code = "",
       this.load()
    },
    handleAdd(){
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row){
      console.log('row',row);
      this.form = Object.assign({},row)
      this.dialogFormVisible = true
      this.isEdit = true
    },
    handleDel(id){
      console.log('id',id);
      this.request.delete("/notice/deleteById/"+id).then(res =>{
        if(res){
          this.$notify.success('移除公告成功')
          this.load()
        }else{
          this.$message.error('移除公告失败')
          this.load()
        }
      })
    },
    handleDeleteBatch(){
      let ids = this.multipleSelection.map(v => v.id)
      console.log('ids',ids)
      if(ids.length === 0){
        this.$message.warning('批量删除前,请先勾选若干套餐')
      }else{
        this.request.delete("/setmeal/deleteBatch/",{data:ids}).then(res =>{
          if(res.code === '200'){
            this.$message.success('批量删除鲜花套餐成功!')
            this.load()
          }else{
            this.$message.error('批量删除鲜花套餐失败!')
            this.load()
          }
        })
      }
    },
    handleSelectionChange(val){
      this.multipleSelection = val
    },
    save(){
      if(this.isEdit === false){
        console.log('add');
        this.request.post("/setmeal/save",this.form).then(res =>{
        if(res){
          this.$message.success('添加套餐成功!')
          this.dialogFormVisible = false
          this.load()
        }else{
          this.$message.error('添加套餐失败!')
          this.dialogFormVisible = false
          this.load()
        }
      })
      }else{
        console.log('edit');
        this.request.put("/setmeal/edit",this.form).then((res) =>{
          if(res){
            this.$message.success("修改套餐信息成功!")
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }else{
            this.$message.error('修改套餐信息失败!')
            this.dialogFormVisible = false
            this.isEdit = false
            this.load()
          }
        })
      }
    },
    cancel(){
      this.dialogFormVisible = false
      this.load()
    },
    exp(){
      window.open("http://8.138.58.18:7777/setmeal/export")
    },
    changeStatus(row){
      this.request.put("/setmeal/changeStatus/"+row.id).then(res =>{
        if(res.code === '200'){
          if(res.data === "起售"){
            this.$message.success('套餐起售成功!')
            this.load()
          }else{
            this.$message.success('套餐停售成功!')
            this.load()
          }
        }else{
          this.$message.error('修改套餐销售状态失败!')
          this.load()
        }
      })
      // console.log('row1',row)
      // console.log('this.tableData',this.tableData)
      // console.log('status1',row.status);
      // if(row.status === "起售"){
      //   console.log('1');
      //   // this.form.status = "停售"
      //   console.log('this.tableData[0].status',this.tableData[0].status);
      //   this.tableData[0].status = "停售"
      // }else{
      //   console.log('2');
      //   this.form.status = "起售"
      // }
      // this.load()
      // console.log('row2',row);
      // console.log('status2',row.status);
    }
  }
};
</script>

<style>
.headerBg4 {
  background-color: rgb(225, 204, 235) !important;
}
</style>